package com.gitee.ed.arithmetic.leetcode.answer.answer_1_100.answer_1_10;

import com.gitee.ed.arithmetic.leetcode.solution.solution_1_100.solution_1_10.Solution6;

/**
 * @author hongda.li
 */
public class Answer6 implements Solution6 {
    @Override
    public String convert(String s, int numRows) {
        if (numRows == 1) {
            return s;
        }

        StringBuilder ans = new StringBuilder();

        int n = s.length();

        for (int i = 0; i < numRows; i++) {
            int k = i;
            while (k < n) {
                ans.append(s.charAt(k));
                k += 2 * (numRows - 1);
                if (i != 0 && i != numRows - 1 && k - 2 * i < n) {
                    ans.append(s.charAt(k - 2 * i));
                }
            }
        }

        return ans.toString();
    }
}
